<?php
namespace app\admin\controller;
use think\Controller;
class Course extends Controller
{
    public function __construct()
    {
        parent::__construct();
        session_start();
        // $username = 
        // $username = $_SESSION['username'];
        if(empty($_SESSION['username'])) {
            // header('Location: http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index');die;
            $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index', '请登录');die;
        }
        
         
    }
   public function index() {
       $corRes = model('Course')->where('status',1)->order('id desc')->paginate(15);
       $this->assign([
           'corRes'=>$corRes
       ]);
       return view('list');
   }
   public function add() {
    if(request()->isPost()) {
        $data = input('post.');
        $corModel = model('Course');
        if(!empty($_FILES['cor_pic']['tmp_name'])) {
                $imgSrc = self::upload('cor_pic');
                // dump($imgSrc); die;
                $data['cor_pic'] = $imgSrc;
            }
            $add = $corModel->save($data);
            if($add) {
                $this->success('添加商品成功', 'index');
            }else {
                $this->error('添加商品失败');
            }
        }

        $cateRes = model('Cate')->where('status',1)->select();
        $this->assign([
            'cateRes'=>$cateRes
        ]);
        return view();
   }
    //编辑
    public function edit() {
        $corModel = model('Course');
        $id = input('id');
        $course = $corModel->find($id);
        if(request()->isPost()) {
            $data = input('post.');
            if($_FILES['cor_pic']['tmp_name']) {
                //删除
                $oldImgSrc = '../public/static/api/uploads/'.$course['cor_pic'];
                if(file_exists($oldImgSrc)) {
                    unlink($oldImgSrc);
                }
                //上传
                $imgSrc = $this->upload('cor_pic');
                $data['icon'] = $imgSrc;
            }
            $save = $corModel->save($data,['id'=>$id]);
            if($save !== false) {
             $this->success('修改成功', 'index');
            }else {
             $this->success('添加失败');
            }
        }else {
            $cateRes = model('Cate')->where('status',1)->select();
            $this->assign([
                'course'=>$course,
                'cateRes'=>$cateRes
            ]);
            return view();
        }
    }
    //删除按钮
    public function del($id) {
        $del = model('Course')->save(['status'=>0],['id'=>$id]);
        if($del) {
         $this->success('删除成功', 'index');
        }else {
         $this->success('删除失败');
        }
    }
    //图片上传
    public static function upload($imgName){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($imgName);
        // 移动到框架应用根目录/uploads/ 目录下
        $uploadsSrc = '../public/static/api/uploads';
        $info = $file->move($uploadsSrc);
        if($info){
            return $info->getSaveName(); 
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }    
}